package com.example.iotest.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
public class Client02 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.101.2", 8081);
        InputStream inputStream = socket.getInputStream();
        File file = new File("J:\\test.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int length;
        log.info("开始下载");
        byte[] buf = new byte[64];
        int read = inputStream.read(buf);
        System.out.println(read);
        System.out.println(new String(buf, 0, buf.length));
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < buf.length; i++) {
            if (buf[i] >= '0' && buf[i] <= '9') {
                s.append((char) buf[i]);
            }
        }
        System.out.println(s);
        long fileLength = Long.parseLong(s.toString());
        AtomicInteger start = new AtomicInteger();
        AtomicLong currentLength = new AtomicLong(file.length());
        AtomicLong startLength = currentLength;
        AtomicLong maxLength = new AtomicLong(currentLength.get() + 1);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
//            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//            System.out.println(file.length()/(1024*1024));
            long l = file.length() - currentLength.get();
            currentLength.set(file.length());
            int andIncrement = start.getAndIncrement();
            if (file.length() != maxLength.get()) {
                System.out.print(l + " B/S   ");
                System.out.print(l / 1024 + " KB/S   ");
                System.out.print(l / 1024 / 1024 + " MB/S   ");

            } else {
                System.out.println("总共花费" + andIncrement + "秒");
                long l1 = startLength.get();
                System.out.println("平均速度" + l1 / andIncrement / 1024 + " KB/S");
                scheduledExecutorService.shutdown();
            }
            System.out.print("[");
            double l1 = ((double) file.length() / (double) fileLength) * 100;
            String[] split = (l1 + "").split("\\.");
            String percent = split[0] + "." + split[1].substring(0, 2);
//            System.out.println(percent);
            System.out.print(percent);
            System.out.println("%]");
            maxLength.set(file.length());

        }, 1, 1, TimeUnit.SECONDS);

        while ((length = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        inputStream.close();
        fileOutputStream.close();
        log.info("文件下载成功!");
        socket.close();
    }
}
